package com.koro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.koro.entity.LoginUser;
import com.koro.entity.Users;
import com.koro.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class LoginUsersService implements UserDetailsService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("type",0);
        Users users = usersMapper.selectOne(queryWrapper);
        if(users == null) {
            throw new UsernameNotFoundException("username:" + username + "not found.");
        }
        return new LoginUser(users);
    }
}
